package com.atguigu.gmall.controller;

import com.atguigu.gmall.entity.BaseBrand;
import com.atguigu.gmall.result.RetVal;
import com.atguigu.gmall.service.BrandService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URL;
import java.util.List;

@RestController
@RequestMapping("/product/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Value("${fastdf.prefix}")
    String fastdfprefix;

    @GetMapping("queryBrandByPage/{currentPage}/{limit}")
    public RetVal queryBrandByPage(@PathVariable Long currentPage, @PathVariable Long limit) {
        Page<BaseBrand> baseBrandPage = new Page<>(currentPage, limit);
        IPage<BaseBrand> page = brandService.selectBrandByPage(baseBrandPage);
        return RetVal.ok(page);
    }
    @GetMapping("{id}")
    public RetVal getById(@PathVariable Long id){
        BaseBrand baseBrand = brandService.getById(id);
        return RetVal.ok(baseBrand);
    }
    /*根据id 获取品牌*/
    @GetMapping("getBrandById/{id}")
    public BaseBrand getBrandById(@PathVariable Long id){
        return brandService.getById(id);
    }
    @GetMapping("getAllBrand")
    public RetVal getAllBrand(){
        List<BaseBrand> baseBrands = brandService.getAllBrand();
        return RetVal.ok(baseBrands);
    }
    /*新增或修改*/
    @PostMapping("/")
    public RetVal saveById(@RequestBody BaseBrand baseBrand){
        brandService.save(baseBrand);
        return RetVal.ok();
    }
    @PutMapping()
    public RetVal updateById(@RequestBody BaseBrand baseBrand){
        brandService.updateById(baseBrand);
        return RetVal.ok();
    }
    @DeleteMapping("{id}")
    public RetVal deleteById(@PathVariable Long id){
        brandService.removeById(id);
        return RetVal.ok();
    }

    /*文件上传*/
    @PostMapping("fileUpload")
    public RetVal fileUpload(MultipartFile file) throws IOException, MyException {
        String path = uploadFile(file);
        return RetVal.ok(path);
    }

    public String uploadFile(MultipartFile file) throws IOException, MyException {
        String conf = this.getClass().getResource("/tracker.conf").getFile();
        ClientGlobal.init(conf);
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        StorageClient1 storageClient = new StorageClient1(trackerServer,null);
        String originalFilename = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(originalFilename);//获取文件名后缀
        String s = storageClient.upload_file1(file.getBytes(), extension, null);
        return fastdfprefix + s;
    }

}
